package de.akabahn.ebd.control.auth;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class GenerateHash {

	public static void main(String[] args) throws Exception {
		if (args.length == 0) {
			System.err.println("Usage: GenerateHash <password> [-q]");
		} else {
			if (args.length == 1) {
				System.out.println(args[0] + ": " + md5hash(args[0]));
			} else {
				System.out.println(md5hash(args[0]));
			}
		}
	}

	static String md5hash(String password) {
		MessageDigest messageDigest = null;
		try {
			messageDigest = MessageDigest.getInstance("SHA-256");
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}

		byte[] digest = messageDigest.digest(password.getBytes());
		StringBuilder sBuilder = new StringBuilder();
		for (byte d : digest) {
			sBuilder.append(Integer.toHexString(d & 0xFF));
		}

		return sBuilder.toString();
	}
}
